using System;
using System.Xml;

namespace OrderSystem.SystemFramework
{
	public static class AppSettings
	{
		private static string m_s_connectionString = "";

		private static string m_s_filePath = "";

		public static string ConnectionString
		{
			get
			{
				return AppSettings.m_s_connectionString;
			}
			set
			{
				AppSettings.m_s_connectionString = value;
			}
		}

		public static string FilePath
		{
			get
			{
				return AppSettings.m_s_filePath;
			}
			set
			{
				AppSettings.m_s_filePath = value;
				if (AppSettings.m_s_filePath.Substring(AppSettings.m_s_filePath.Length - 1, 1) != "\\")
				{
					AppSettings.m_s_filePath += "\\";
				}
			}
		}

		public static string GetAppSettings(string key)
		{
			string result = "";
			string xml = "";
			XmlTextReader xmlTextReader = new XmlTextReader(AppSettings.m_s_filePath + "configuration.xml");
			while (xmlTextReader.Read())
			{
				if (xmlTextReader.NodeType == XmlNodeType.Element && xmlTextReader.LocalName == "appSettings")
				{
					xml = xmlTextReader.ReadOuterXml();
					break;
				}
			}
			xmlTextReader.Close();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(xml);
			foreach (XmlNode xmlNode in xmlDocument.DocumentElement.ChildNodes)
			{
				if (xmlNode.LocalName == "add" && xmlNode.Attributes["key"].Value == key)
				{
					result = xmlNode.InnerText;
					break;
				}
			}
			return result;
		}

		public static void SetAppSettings(string key, string setValue)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(AppSettings.m_s_filePath + "configuration.xml");
			XmlElement documentElement = xmlDocument.DocumentElement;
			documentElement.SelectSingleNode("child::appSettings").SelectSingleNode("//add[@key='" + key + "']").InnerText = setValue;
			xmlDocument.Save(AppSettings.m_s_filePath + "configuration.xml");
		}

		public static string GetExcelSettings(string outType)
		{
			string result = "";
			string xml = "";
			XmlTextReader xmlTextReader = new XmlTextReader(AppSettings.m_s_filePath + "configuration.xml");
			while (xmlTextReader.Read())
			{
				if (xmlTextReader.NodeType == XmlNodeType.Element && xmlTextReader.LocalName == "excelSettings")
				{
					xml = xmlTextReader.ReadOuterXml();
					break;
				}
			}
			xmlTextReader.Close();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(xml);
			XmlNode xmlNode = xmlDocument.DocumentElement.SelectSingleNode("child::" + outType);
			string value = xmlNode.Attributes["defaultVersion"].Value;
			foreach (XmlNode xmlNode2 in xmlNode.ChildNodes)
			{
				if (xmlNode2.LocalName == "add" && xmlNode2.Attributes["version"].Value == value)
				{
					result = xmlNode2.InnerText;
					break;
				}
			}
			return result;
		}

		public static string GetDefaultVersionOfOutType(string outType)
		{
			string xml = "";
			XmlTextReader xmlTextReader = new XmlTextReader(AppSettings.m_s_filePath + "configuration.xml");
			while (xmlTextReader.Read())
			{
				if (xmlTextReader.NodeType == XmlNodeType.Element && xmlTextReader.LocalName == "excelSettings")
				{
					xml = xmlTextReader.ReadOuterXml();
					break;
				}
			}
			xmlTextReader.Close();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(xml);
			XmlNode xmlNode = xmlDocument.DocumentElement.SelectSingleNode("child::" + outType);
			return xmlNode.Attributes["defaultVersion"].Value;
		}

		public static void SetDefaultVersionOfOutType(string outType, string newValue)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(AppSettings.m_s_filePath + "configuration.xml");
			XmlElement documentElement = xmlDocument.DocumentElement;
			documentElement.SelectSingleNode("child::excelSettings/child::" + outType).Attributes["defaultVersion"].Value = newValue;
			xmlDocument.Save(AppSettings.m_s_filePath + "configuration.xml");
		}
	}
}
